package com.usian.controller;

import com.usian.SSOFeign;
import com.usian.pojo.TbUser;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/frontend/sso")
public class SSOController {

    @Autowired
    private SSOFeign ssoFeign;

    /**
     * 用户注册与登录
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @RequestMapping("/checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,
                                @PathVariable Integer checkFlag){
      Boolean checkUser =ssoFeign.checkUserInfo(checkValue,checkFlag);
        if (checkUser){
            return Result.ok(checkUser);
        }
        return Result.error("校验失败");
    }


    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("/userRegister")
    public Result userRegister(TbUser user){
        Integer userNum = ssoFeign.userRegister(user);
        if (userNum == 1){
            return Result.ok();
        }
        return Result.error("注册失败");
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/userLogin")
    public Result userLogin(@RequestParam String username,@RequestParam String password){
        Map map = ssoFeign.userLogin(username,password);
        if (map != null){
            return Result.ok(map);
        }
        return Result.error("登陆失败");
    }

    /**
     * 查询用户是否过期
     * @param token
     * @return
     */
    @RequestMapping("/getUserByToken/{token}")
    public Result getUserByToken(@PathVariable String token){
        TbUser tbUser = ssoFeign.getUserByToken(token);
        if (tbUser != null){
            return Result.ok();
        }
        return Result.error("登录过期");
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @RequestMapping("/logOut")
    public Result logOut(@RequestParam String token){
        Boolean logOut = ssoFeign.logOut(token);
        if (logOut){
            return Result.ok();
        }
        return Result.error("退出失败");
    }
}
